import pdb,sys,os,imp

def __import__(name, globals=None, locals=None, fromlist=None):
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module(name)

    try:
        return imp.load_module(name, fp, pathname, description)
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close()


dirname = '\\'.join(sys.argv[1].split('\\')[:-1]) + '\\'
lineno = sys.argv[2]
mname = sys.argv[3][:-3]

print 'Dir:',dirname
print 'Module:',mname
print 'Line number:',lineno

#import all moduls under the working directory
pathtoappend = set([])
bdict = {}
for root,dirs,files in os.walk(dirname):
        for f in files:
                if f[-3:] == '.py':
                    files.append(f[:-3])
                    pathtoappend.add(root)
                    bdict[f] = root
                
        
for s in pathtoappend:
        sys.path.insert(0,s)

print bdict
for k in bdict:
     try:
        os.chdir(bdict[k])
        __import__(k[:-3])
     except:
        print k[:-3],' not imported'
        continue
        

m = __import__(mname)

print str(m)
print dir(m)
pdb.run('m')